﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cgd.Shell.Data;
using System.Collections.Generic;

public partial class RssDoc : System.Web.UI.Page
{
	protected CgdColumn _pColumn;

	protected void Page_Init(object sender, EventArgs e)
	{
		try
		{
			int c = int.Parse(Request["c"]);
			_pColumn = CgdColumn.GetByValue(c);
			this.Title = _pColumn.Title;
			lblColumn.Text = this.Title;
		}
		catch
		{
			Response.StatusCode = 404;
			Response.End();
			return;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
			BindData();
	}

	private void BindData()
	{
		using (CgdShellDataContext db = new CgdShellDataContext())
		{
			var q = from rssdoc in db.RssDocuments
					from rssfeed in db.RssFeeds
					where rssfeed.Id == rssdoc.RssFeedId && rssfeed.Category == _pColumn.Value
					orderby rssdoc.Id
					select new
					{
						Id = rssdoc.Id,
						Title = rssdoc.Title,
						Url = rssdoc.Url,
						Summary = rssdoc.Summary,
						RssTitle = rssfeed.Title,
						RssUrl = rssfeed.Url
					};

			ListView1.DataSource = q;
			ListView1.DataBind();
		}
	}
	protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
	{
		Guid id = new Guid(e.CommandArgument.ToString());
		if (e.CommandName == "AddToDocument")
			AddToDocument(new[] { id });
		else if (e.CommandName == "DeleteFormLibrary")
			DeleteFromLibrary(new[] { id });

		BindData();
	}

	private void AddToDocument(Guid[] ids)
	{
		using (CgdShellDataContext db = new CgdShellDataContext())
		{
			var q = from rd in db.RssDocuments
					where ids.Contains(rd.Id)
					select rd;

			if (q.Count() != 0)
			{
				foreach (RssDocument rdoc in q)
				{
					Document doc = new Document()
					{
						Id = Guid.NewGuid(),
						Category = _pColumn.Value,
						Title = rdoc.Title,
						Url = rdoc.Url,
						Summary = rdoc.Summary,
						Time = DateTime.Now
					};

					db.Documents.InsertOnSubmit(doc);
					db.RssDocuments.DeleteOnSubmit(rdoc);
				}
				db.SubmitChanges();
			}
		}
	}

	private void DeleteFromLibrary(Guid[] ids)
	{
		using (CgdShellDataContext db = new CgdShellDataContext())
		{
			var q = from rd in db.RssDocuments
					where ids.Contains(rd.Id)
					select rd;

			if (q.Count() != 0)
			{
				db.RssDocuments.DeleteAllOnSubmit(q);
				db.SubmitChanges();
			}
		}
	}
	protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
	{
		if (e.Item.ItemType == ListViewItemType.DataItem)
		{
			CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
			ListViewDataItem item = (ListViewDataItem)e.Item;
			string id = ListView1.DataKeys[item.DataItemIndex].Value.ToString();
			chkSelect.InputAttributes.Add("value", id);
		}
	}
	protected void btnAddSelected_Click(object sender, EventArgs e)
	{
		var q = from field in Request.Form.AllKeys
				where field.Contains("chkSelect")
				select field;

		List<string> idss = new List<string>();
		foreach (var s in q)
		{
			idss.Add(Request.Form[s]);
		}
		var ids = from id in idss
				  select new Guid(id);

		AddToDocument(ids.ToArray());
		BindData();
	}
	protected void btnAddAll_Click(object sender, EventArgs e)
	{
		using (CgdShellDataContext db = new CgdShellDataContext())
		{
			var q = from rd in db.RssDocuments
					select rd;

			if (q.Count() != 0)
			{
				foreach (RssDocument rdoc in q)
				{
					Document doc = new Document()
					{
						Id = Guid.NewGuid(),
						Category = _pColumn.Value,
						Title = rdoc.Title,
						Url = rdoc.Url,
						Summary = rdoc.Summary,
						Time = DateTime.Now
					};

					db.Documents.InsertOnSubmit(doc);
					db.RssDocuments.DeleteOnSubmit(rdoc);
				}
				db.SubmitChanges();
			}
		}
		BindData();
	}
	protected void btnDeleteSelected_Click(object sender, EventArgs e)
	{
		var q = from field in Request.Form.AllKeys
				where field.Contains("chkSelect")
				select field;

		List<string> idss = new List<string>();
		foreach (var s in q)
		{
			idss.Add(Request.Form[s]);
		}
		var ids = from id in idss
				  select new Guid(id);

		DeleteFromLibrary(ids.ToArray());
		BindData();
	}
	protected void btnDeleteAll_Click(object sender, EventArgs e)
	{
		using (CgdShellDataContext db = new CgdShellDataContext())
		{
			var q = from rd in db.RssDocuments
					select rd;

			if (q.Count() != 0)
			{
				db.RssDocuments.DeleteAllOnSubmit(q);
				db.SubmitChanges();
			}
		}
		BindData();
	}
}
